package com.ruoyi.ldzlsc.listener;

import com.ruoyi.ldzlsc.domain.ScWorkOrderTask;
import com.ruoyi.ldzlsc.domain.dto.WorkReportDto;
import com.ruoyi.ldzlsc.mapper.ScWorkOrderTaskMapper;
import com.ruoyi.ldzlsc.service.WebSocketService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.math.BigDecimal;

/**
 * 质检结果模拟器
 * 用于模拟质检完成事件，触发回调控制器
 * 
 * @author ldzl
 * @date 2025-01-23
 */
@Component
public class QualityCheckResultListener {
    
    @Autowired
    private WebSocketService webSocketService;
    
    /**
     * 模拟质检完成事件（用于测试）
     * 实际应该通过RabbitMQ或其他消息机制触发
     */
    public void simulateQualityCheckComplete(Long taskId, String checkResult, BigDecimal goodQty) {
        // 模拟质检单ID
        Long qualityCheckId = System.currentTimeMillis();
        
        // 延迟处理，模拟质检时间
        new Thread(() -> {
            try {
                Thread.sleep(3000); // 3秒后完成质检
                
                // 推送质检完成消息
                webSocketService.pushQualityCheckComplete(
                    taskId,
                    qualityCheckId,
                    checkResult,
                    "模拟质检完成，结果: " + (checkResult.equals("1") ? "合格" : "不合格")
                );
                
                System.out.println("=== 模拟质检完成，请调用质检结果回调接口手动处理 ===");
                System.out.println("可以通过访问 /sc/quality-check/manual-complete 接口手动触发处理");
                
            } catch (InterruptedException e) {
                Thread.currentThread().interrupt();
            }
        }).start();
    }
}
